package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @PostMapping("queryAllPage")
    public CommonVO queryAllPage(@RequestBody PageDTO pageDTO){
        log.info("当前页: {}",pageDTO);
        CommonVO commonVO = userService.pageQueryAll(pageDTO);
        log.info("页面展示用户分页数据 {}",commonVO);
        return commonVO;
    }

    /**
     * 修改状态
     */
    @PostMapping("update")
    public CommonMessageVO update(@RequestBody User user){
        log.info("user : {}",user);
        CommonMessageVO messageVO = userService.update(user);
        return messageVO;
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    public CommonMessageVO delete(@RequestBody User user){
        log.info("user : {}",user);
        CommonMessageVO delete = userService.delete(user);
        return delete;
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public CommonMessageVO add(@RequestBody User user){
        log.info("user---------- : {}",user);
        //用户添加
        CommonMessageVO insert = userService.insert(user);
        return insert;
    }

    /**
     * 文件上传
     */
    @PostMapping("uploadHeadImg")
    public void uploadHeadImg(MultipartFile headImg){
        log.info("获取文件名 {}",headImg.getOriginalFilename());
        log.info("获取文件类型 {}",headImg.getContentType());
        log.info("获取文件大小 {}",headImg.getSize());
        String uploadHeadImg = userService.uploadHeadImg(headImg);
    }
    /**
     * 修改用户前的查询，根据id查询
     */
    @GetMapping("queryById")
    public User queryById(User user){
        log.info("user : {}",user);
        User users = userService.queryById(user);
        return users;
    }
}
